Scroll - cast some magic
_______________
()==( (@==()
'______________'|
| |
| ἀρετή |
__)_____________|
()==( (@==()
'--------------'
Documentation
Usage
Add to your Cargo.toml
[]
= "0.2.0"
Overview
Scroll implements several traits for read/writing generic containers (byte buffers are currently implemented by default). Most familiar will likely be the Pread
trait, which at its basic takes an immutable reference to self, an immutable offset to read at, (and a parsing context, more on that later), and then returns the deserialized value.
Because self is immutable, all reads can be performed in parallel and hence are trivially parallelizable.
A simple example demonstrates its flexibility:
use ;
let bytes: = ;
// we can use the Buffer type that scroll provides, or use it on regular byte slices (or anything that impl's `AsRef<[u8]>`)
//let buffer = scroll::Buffer::new(bytes);
let b = &bytes;
// reads a u32 out of `b` with Big Endian byte order, at offset 0
let i: u32 = b.pread_with.unwrap;
// or a u16 - specify the type either on the variable or with the beloved turbofish
let i2 = b..unwrap;
// We can also skip the ctx by calling `pread`.
// for the primitive numbers, this will default to the host machine endianness (technically it is whatever default `Ctx` the target type is impl'd for)
let byte: u8 = b.pread.unwrap;
let i3: u32 = b.pread.unwrap;
// this will have the type `scroll::Error::BadOffset` because it tried to read beyond the bound
let byte: Result = b.pread;
// we can also get str and byte references from the underlying buffer/bytes using `pread_slice`
let slice = b..unwrap;
let byte_slice: & = b.pread_slice.unwrap;
// here is an example of parsing a uleb128 custom datatype, which
// uses the `ctx::DefaultCtx`
let leb128_bytes: = ;
// parses a uleb128 (variable length encoded integer) from the above bytes
let uleb128: u64 = leb128_bytes..unwrap.into;
assert_eq!;
// finally, we can also parse out custom datatypes, or types with lifetimes
// if they implement the conversion trait `TryFromCtx`; here we parse a C-style \0 delimited &str (safely)
let hello: & = b"hello_world\0more words";
let hello_world: &str = hello.pread.unwrap;
assert_eq!;
// ... and this parses the string if its space separated!
let spaces: & = b"hello world some junk";
let world: &str = spaces.pread_with.unwrap;
assert_eq!;
Advanced Uses
Scroll is designed to be highly configurable - it allows you to implement various context (Ctx
) sensitive traits, which then grants the implementor automatic uses of the Pread
/Gread
and/or Pwrite
/Gwrite
traits.
For example, suppose we have a datatype and we want to specify how to parse or serialize this datatype out of some arbitrary
byte buffer. In order to do this, we need to provide a TryFromCtx
impl for our datatype.
In particular, if we do this for the [u8]
target, using the convention (usize, YourCtx)
, you will automatically get access to
calling pread::<YourDatatype>
on arrays of bytes.
use ;
// we could use a `(usize, endian::Scroll)` if we wanted
// note the lifetime specified here
let bytes = new;
let data = bytes..unwrap;
assert_eq!;
assert_eq!;
Please see the official documentation, or a simple example for more.
Contributing
There are several open issues right now which I'd like clarified/closed before releasing on crates.io. Keep in mind, the primary use case is an immutable byte parser/reader, which Pread
implements, and which I want backwards compability at this point.
In fact, if you look at the tests, most of them actually are just testing the APIs remain unbroken (still compiling), which is very easy to do with something this generic.
However, I believe there are some really interesting ideas to pursue, particularly in terms of the more generic contexts that scroll allows.
Any ideas, thoughts, or contributions are welcome!